import http from "http";
import koa from "koa";
import bodyparser from "@koa/bodyparser";
import staticServer from "koa-static";
import { recover, expand, token } from "./middlewares";
import { registerRoutes } from "./router/register";
import { routerManager } from "./router/router";
import path from "path";
import { utils } from "./utils";
import "./db/db";

const app = new koa();

app.use(bodyparser());

// 注册静态服务
app.use(staticServer(path.resolve(__dirname, "..", "..", "render")));

// 注册全局中间件
app.use(expand);
app.use(recover);
app.use(token);

// 注册路由
registerRoutes(path.resolve(__dirname, "service"));
app.use(routerManager.routes());

const server = http.createServer(app.callback());

// 注册Websocket
server.listen(3000, () => {
  console.log(`服务启动在:3000`);
  utils.Logger.info(`服务启动在:3000`);
});
